var NH_Score = function () {

    return {
        init: function () {
       
            $modal = $('#responsive');
            $.fn.modalmanager.defaults.resize = true;
            $.fn.modalmanager.defaults.spinner = '<div class="loading-spinner fade" style="width: 200px; margin-left: -100px;"><img src="assets/img/ajax-modal-loading.gif" align="middle">&nbsp;<span style="font-weight:300; color: #eee; font-size: 18px; font-family:Open Sans;">&nbsp;Đang tải...</span></div>';
            
            
            $('td.hocluc').click(function () {
                $('body').modalmanager('loading');

                var obj = {};
                obj['userid'] = $(this).attr('userid');
                obj['username'] = $(this).attr('username');
                obj['score'] = $(this).attr('score');
                obj['data'] = $(this).attr('data');
                obj['scoretype'] = $(this).attr('scoretype');
                
                $modal.load('score_modal.php', obj, function(){
                    $modal.modal({
                        focusOn: 'input:text'
                    }).on("hidden", function() {
                        $modal.empty();
                    });
                },'json');
            });

            $modal.on('click', '.btn-save', function () {
                    
                var tmp_inputs = $("input, select, textarea", $modal);
                var obj = {};
                    
                $.map(tmp_inputs, function(n, i) {
                    obj[$(n).attr('name')] = $(n).val();
                });
                    
                $('[userid="'+obj.userid+'"][data="'+obj.data+'"][scoretype="'+obj.scoretype+'"][class*="hocluc"]')
                .html('<img src="assets/img/loading.gif"/>')
                    
                $.ajax({
                    url: 'score_execute.php?action=update-score',
                    type: 'post',
                    dataType: 'json',
                    data: $.param(obj),
                    success:function(response){
                        if (response.code = 200) {
                                
                            $('[userid="'+response.data.userid+'"][data="'+response.data.data+'"][scoretype="'+response.data.scoretype+'"][class*="hocluc"]')
                            .html(response.data.scorevalue)
                            .attr('score', response.data.scorevalue);
                        }
                    }
                });
                    
                $('.close',$modal).click();
            })
            .on('keypress', 'input:text', function (e) {
                //if the letter is not digit then display error and don't type anything
                if (e.which != 8 && e.which != 46 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                        
                    if (e.which == 13) {
                        $('.btn-save', $modal).trigger('click');
                    }
                    return false;
                }
            });
        }
    };
}();